package com.helasasa.maxfunds.lineofcredit.cashloan.cameroon.screen.review

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.helasasa.maxfunds.lineofcredit.cashloan.ProjectApplication
import com.helasasa.maxfunds.lineofcredit.cashloan.cameroon.http.IApiProcessCameroonCallback
import com.helasasa.maxfunds.lineofcredit.cashloan.cameroon.http.createApiActionCameroon
import com.helasasa.maxfunds.lineofcredit.cashloan.cameroon.http.doPostRequestCameroon
import org.json.JSONArray
import org.json.JSONObject
import java.math.BigDecimal

class ReviewCameroonViewModel : ViewModel() {

    private val _loadingResult = MutableLiveData<Boolean>()
    val loadingResult: LiveData<Boolean> = _loadingResult

    private val _pageDetailResult = MutableLiveData<ReviewCameroonResult>()
    val pageDetailResult: LiveData<ReviewCameroonResult> = _pageDetailResult

    private val _walletListResult = MutableLiveData<ReviewCameroonWalletListResult>()
    val walletListResult: LiveData<ReviewCameroonWalletListResult> = _walletListResult

    private val _commendProductsResult = MutableLiveData<ReviewCameroonCommendProductsResult>()
    val commendProductsResult: LiveData<ReviewCameroonCommendProductsResult> =
        _commendProductsResult

    private val _reviewDialogResult = MutableLiveData<ReviewCameroonReviewDialogResult>()
    val reviewDialogResult: LiveData<ReviewCameroonReviewDialogResult> = _reviewDialogResult

    /** 获取页面详情接口 */
    fun pageDetail() {
        _loadingResult.value = true
        val context = ProjectApplication.context
        val action = hashMapOf<String, Any>().createApiActionCameroon()
        action["path"] = "WvSc3ghpalrgbjmUXBKvvg"

        context.doPostRequestCameroon(action, object : IApiProcessCameroonCallback {
            override fun onApiReady() {
            }

            override fun onApiDone(jsonObject: JSONObject) {
                _loadingResult.value = false
                val obj = jsonObject.optJSONObject("obj") ?: JSONObject()
                // 去掉无效0
                val amount = obj.optString("amount")
                val amountString = BigDecimal(amount).stripTrailingZeros().toPlainString()
                obj.put("amount", amountString)

                val currency = obj.optString("currency")
                val amountFormat = "$amountString $currency"
                obj.put("amountFormat", amountFormat)

                val days = obj.optString("days")
                obj.put("daysFormat", "$days jours")

                _pageDetailResult.value = ReviewCameroonResult(success = obj)
            }

            override fun onApiError(code: String, errMsg: String) {
                _loadingResult.value = false
                _pageDetailResult.value = ReviewCameroonResult(error = errMsg)
            }
        })
    }

    /**
     * 获取卡列表
     */
    fun walletList() {
        _loadingResult.value = true
        val context = ProjectApplication.context
        val bankcardAction = hashMapOf<String, Any>().createApiActionCameroon()
        bankcardAction["path"] = "wzutROeKRHbLUVhk4MDWJw"
        context.doPostRequestCameroon(bankcardAction, object : IApiProcessCameroonCallback {
            override fun onApiReady() {
            }

            override fun onApiDone(jsonObject: JSONObject) {
                _loadingResult.value = false

                val obj = jsonObject.optJSONArray("obj") ?: JSONArray()

                val list = mutableListOf<JSONObject>()
                for (index in 0 until obj.length()) {
                    val item = obj[index] as JSONObject
                    list.add(item)
                }

                val showAdd = list.size <= 1

                _walletListResult.value =
                    ReviewCameroonWalletListResult(items = list, showAdd = showAdd)
            }

            override fun onApiError(code: String, errMsg: String) {
                _loadingResult.value = false

                _walletListResult.value = ReviewCameroonWalletListResult(error = errMsg)
            }
        })
    }


    /** 获取推荐产品接口 */
    fun commendProducts() {
        val context = ProjectApplication.context
        val action = hashMapOf<String, Any>().createApiActionCameroon()
        action["path"] = "na7K1WhPIBpMqSF635Spgg"

        context.doPostRequestCameroon(action, object : IApiProcessCameroonCallback {
            override fun onApiReady() {
            }

            override fun onApiDone(jsonObject: JSONObject) {
                val obj = jsonObject.optJSONObject("obj") ?: JSONObject()
                val dcProductList = obj.optJSONArray("dcProductList") ?: JSONArray()
                val list = mutableListOf<JSONObject>()
                for (index in 0 until dcProductList.length()) {
                    val item = dcProductList[index] as JSONObject
                    list.add(item)
                }
                _commendProductsResult.value =
                    ReviewCameroonCommendProductsResult(success = list)
            }

            override fun onApiError(code: String, errMsg: String) {
                _commendProductsResult.value =
                    ReviewCameroonCommendProductsResult(error = errMsg)
            }
        })
    }

    /** 获取评价框配置接口 */
    fun reviewDialog() {
        val context = ProjectApplication.context
        val action = hashMapOf<String, Any>().createApiActionCameroon()
        action["path"] = "x8CIlIS4cYCKGUJZ2lpqEg"

        context.doPostRequestCameroon(action, object : IApiProcessCameroonCallback {
            override fun onApiReady() {
            }

            override fun onApiDone(jsonObject: JSONObject) {
                val obj = jsonObject.optJSONObject("obj") ?: JSONObject()
                val activeWindow = obj.optJSONObject("activeWindow") ?: JSONObject()
                val normalWindow = obj.optJSONObject("normalWindow") ?: JSONObject()

                val activeSwitch = activeWindow.optString("switch", "NO")
                val activeContent = activeWindow.optString("content")
                val activeNum = activeWindow.optString("num", "3").toInt()

                val normalSwitch = normalWindow.optString("switch", "NO")
                val normalContent = normalWindow.optString("content")
                val normalNum = normalWindow.optString("num", "3").toInt()

                // 已展示次数
                val didShowCount = ProjectApplication.reviewDialogAction(isGet = true)

                if (activeSwitch == "YES") {
                    if (activeNum > didShowCount) {
                        _reviewDialogResult.value =
                            ReviewCameroonReviewDialogResult(boldDialog = activeContent)
                        return
                    }
                }

                if (normalSwitch == "YES") {
                    if (normalNum > didShowCount) {
                        _reviewDialogResult.value =
                            ReviewCameroonReviewDialogResult(normalDialog = normalContent)
                        return
                    }
                }

                _reviewDialogResult.value = ReviewCameroonReviewDialogResult()
            }

            override fun onApiError(code: String, errMsg: String) {
                _reviewDialogResult.value =
                    ReviewCameroonReviewDialogResult(error = errMsg)
            }
        })
    }
}